<?php

/*
 * 这不是一个开放软件，您不能用于商业应用
 * 不允许对程序代码以任何形式任何目的的再发布。
 * Copyright (c) 2015 YOUM.Co.Ltd. All rights reserved..
 * 网站地址：http://www.huishuojs.com
 * Author Zhyu
 */

/**
 * @author Zhyu
 * @date 2015-12-20 02:21:04
 * @copyright 杭州晖硕有码科技
 */
class User_yeji extends PC_Controller
{
	//put your code here
	protected $go_url = '/admin/user_yeji/index';
	public function __construct()
	{
		parent::__construct();
		$this->load->model('user_yeji_model');
		$this->load->model('user_yeji_log_model');
		$this->load->model('user_model');
	}
	/**
	 * 列表
	 */
	public function index()
	{
		$this->data['username'] =  empty($_POST) ? '' : $_POST['username'];
		//         $this->data['phone'] =  empty($_POST) ? '' : $_POST['phone'];
		$this->data['user_id'] =  isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : '';
		$this->data['tuandui'] =  isset($_REQUEST['tuandui']) ? $_REQUEST['tuandui'] : '';
		$this->data['user_level'] =  isset($_REQUEST['user_level']) ? $_REQUEST['user_level'] : '';
		$this->data['start_time'] = isset($_REQUEST['start_time']) && $_REQUEST['start_time'] ? $_REQUEST['start_time'] : date('Y-m-d', strtotime('-30 day'));
		$this->data['end_time'] = isset($_REQUEST['end_time']) && $_REQUEST['end_time'] ? $_REQUEST['end_time'] : date('Y-m-d');
		//查找所有会员等级
		$this->load->model('user_level_model');
		$levelList= $this->user_level_model->getUserLevelList(array(),100);
		$this->data['levelList'] = $levelList;
		$this->load->view('/admin/user_yeji_list.php',  $this->data);
	}
	/**
	 * 
	 */
	public function ajax_user_yeji_list()
	{
		$data = $_GET;
		$start = $data['iDisplayStart']; //显示的起始索引
		$length = $data['iDisplayLength']; //显示的行数
		$sort_th = $data['mDataProp_' . $data['iSortCol_0'] . '']; //被排序的列
		$sort_type = $data['sSortDir_0']; //排序的方向 "desc" 或者 "asc".
		$where = array();
		/*$search = $data ['sSearch'];//全局搜索字段
        $data['username'] ? $where['username like'] = '%'.trim($data['username']).'%' :'';
        $data['phone'] ? $where['phone like'] = '%'.trim($data['phone']).'%' :'';
        $data['parent_id']?$where['parent_id']=$data['parent_id']:'';
        $where['concat(id) like'] = '%'.trim($search).'%';*/
		// if (!empty($data['username'])) {
		// 	$users = $this->user_model->getUserAll(array('username' => trim($data['username'])));

		// 	if (!empty($users)) {
		// 		$user_ids = array_column($users, 'id');

		// 		$where['user_id in'] = $user_ids;
		// 	} else {
		// 		$this->data['count'] = 0;
		// 		$output['aaData'] = array();
		// 		$output['sEcho'] = $_GET['sEcho'];
		// 		$output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
		// 		$output['iTotalRecords'] = $this->data['count']; //总共有几条数据
		// 		echo json_encode($output);
		// 		exit; //最后把数据以json格式返回
		// 	}
		// }
		!empty($data['username']) ? $where['user.username'] = trim($data['username']) : '';
		!empty($data['user_level']) ? $where['user.user_level'] = trim($data['user_level']) : '';
		!empty($data['user_id']) ? $where['user_yeji.user_id'] = $data['user_id'] : '';
		!empty($data['start_time']) ? $where['user_yeji.day >='] = $data['start_time'] : '';
		!empty($data['end_time']) ? $where['user_yeji.day <='] = $data['end_time'] : '';
		!empty($data['tuandui']) ? $where['tuandui'] = $data['tuandui'] : '';
		$this->data['count'] = $this->user_yeji_model->getCount($where);
		$aaData = array();
		if ($this->data['count']) {
			$order_by = $sort_th . " " . $sort_type;
			$user_yeji_list = $this->user_yeji_model->getList($where, $length, $start, $order_by);
			$this->load->model('user_level_model');
			$user_levels = $this->user_level_model->getUserLevelList();


			//echo $this->db->last_query();
			foreach ($user_yeji_list as $key => &$value) {
				$value['user_level_name'] = '';
				foreach ($user_levels as $uk => $uv) {
					if ($uv['id'] == $value['user_level']) {
						
						$value['user_level'] = '<b style="color:' . $uv['color'] . ';">' . $uv['user_level_name'] . '</b>';
					}
				}
				// $del_oper = "onclick=\"if(confirm('确实要删除此条记录吗？')) return true;else return false; \"";
				// $edit_url = '<a href="/admin/user_yeji/edit_page/' . $value['id'] . '"><span class="btn btn-xs btn-primary">编辑</span></a>';
				// $del_url = '<a href="/admin/user_yeji/del/' . $value['id'] . '"  ' . $del_oper . '><span class="btn btn-xs btn-danger cancel">删除</span></a>';
				$user_yeji_list[$key]['operate'] = '';
			}
			$aaData = $user_yeji_list;
		}
		$output['aaData'] = $aaData;
		$output['sEcho'] = $_GET['sEcho'];
		$output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
		$output['iTotalRecords'] = $this->data['count']; //总共有几条数据
		echo json_encode($output); //最后把数据以json格式返回
	}
	/**
	 * @brief 进入编辑页面
	 * @param Null
	 * @par 2015/10/12 Ver 1.00 Created by Allen
	 */
	public function edit_page()
	{
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$user_yeji_info = $this->user_yeji_model->checkUserLevel($where);

		$this->data['data'] = $user_yeji_info;
		$this->load->view('/admin/user_yeji_edit', $this->data);
	}
	/**
	 * @brief 保存编辑信息
	 * @param Null
	 * @par 2015/10/12 Ver 1.00 Created by Allen
	 */
	public function edit()
	{
		$data = $_POST;
		$where['id'] = $data['id'];
		if ($data['is_default'] == '1 ') {
			$this->user_yeji_model->editUserLevel(array(), array('is_default' => '0'));
		}
		$edit_result = $this->user_yeji_model->editUserLevel($where, $data);

		if ($edit_result) {
			$this->location_href($this->go_url . "/2");
		} else {
			$this->location_href($this->go_url . "/3");
		}
	}

	/**
	 * @brief 删除会员信息
	 * @param Null
	 * @par 2015/10/12 Ver 1.00 Created by Allen
	 */
	public function del()
	{
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$del_result = $this->user_yeji_model->deleteUserLevel($where);
		$go_url = '/admin/user/user_list';
		if ($edit_result) {
			$this->location_href($this->go_url . "/2");
		} else {
			$this->location_href($this->go_url . "/3");
		}
	}
	/**
	 * @brief 检查管理员姓名唯一性
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function check_sole()
	{
		$data = $_POST;
		if (isset($data['id'])) {
			$where['id !='] = $data['id'];
		}
		$where['other_performance'] = $data['other_performance'];
		$where['group_performance'] = $data['group_performance'];
		$sole = $this->user_yeji_model->checkUserLevel($where);
		if ($sole) {
			$message = false;
		} else {
			$message = true;
		}
		echo json_encode($message);
	}
	/**
	 * @brief 进入添加页面
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function add_page()
	{
		$this->load->view('/admin/user_yeji_add', $this->data);
	}

	/**
	 * @brief 保存添加信息
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function add()
	{
		$data = $_POST;
		$add_result = $this->user_yeji_model->addUserLevel($data);
		if ($add_result) {
			$this->location_href($this->go_url . "/4");
		} else {
			$this->location_href($this->go_url . "/5");
		}
	}

	public function reset()
	{
		ignore_user_abort(true);
		ini_set('max_execution_time', '0');
		ini_set('memory_limit', '-1');
		$where = array();
		if (!empty($_GET['keyword'])) {
			if (strtoupper(substr($_GET['keyword'], 0, 1)) == 'P') {
				$where['id'] = substr($_GET['keyword'], 1);
			}
			$userIds = explode('-', $_GET['keyword']);
			$where['id >='] = $userIds[0];
			if (count($userIds) > 1) {
				$where['id <='] = $userIds[1];
			}
		}
		$users = $this->user_model->getFeildList($where, 'id,parent_id,price,create_time', 'id asc');

		foreach ($users as $key => $value) {
			$value['source_user_id'] = $value['id'];
			$this->getYejiLog($value, 1);
		}
		$return = array('num' => count($users));
		echo json_encode($return);
	}
	/**
	 * 添加会员后处理业绩
	 */
	private function getYejiLog($user, $first = 1)
	{

		if ($user['parent_id']) {
			$parent_user = $this->user_model->checkUser(array('user.id' => $user['parent_id']));
			if ($parent_user) {
				//先查找业绩日志是否存在
				$old = $this->user_yeji_log_model->save(array(
					'user_id' => $parent_user['id'],
					'source_user_id' => $user['source_user_id']
				));
				if(empty($old)){
					//保存用户业绩记录
				$this->user_yeji_log_model->save(array(
					'user_id' => $parent_user['id'],
					'source_user_id' => $user['source_user_id'],
					'zhitui' => $first == 1 ? $user['price'] : 0,
					'tuandui' => $user['price'],
					'create_time' => $user['create_time'],
					'day' => date('Y-m-d', $user['create_time']),
				));
				}
				
				if ($parent_user['parent_id'] > 0) {
					$parent_user['source_user_id'] = $user['source_user_id'];
					$parent_user['price'] = $user['price'];
					$parent_user['create_time'] = $user['create_time'];
					$this->getYejiLog($parent_user, 0);
				}
			}
		}
	}
}
